package com.ruoyi.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.api.controller.CourierController;
import com.ruoyi.api.domain.Courier;
import com.ruoyi.api.domain.CourierCompany;
import com.ruoyi.api.domain.CourierCompanyMessage;
import com.ruoyi.api.mapper.CourierCompanyMapper;
import com.ruoyi.api.mapper.CourierCompanyMessageMapper;
import com.ruoyi.api.mapper.CourierMapper;
import com.ruoyi.api.service.ICourierCompanyService;
import com.ruoyi.common.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 寻觅
 * @since 2025-06-13
 */
@Service
public class CourierCompanyServiceImpl extends ServiceImpl<CourierCompanyMapper, CourierCompany> implements ICourierCompanyService {
    @Autowired
    CourierCompanyMapper courierCompanyMapper;

    @Autowired
    CourierMapper courierMapper;

    @Autowired
    CourierCompanyMessageMapper courierCompanyMessageMapper;

    /**
     * 获取快递公司列表
     *
     * @return
     */
    @Override
    public List<CourierCompany> listData() {
        List<CourierCompany> courierCompanies = courierCompanyMapper.selectList(null);

        if (courierCompanies.isEmpty()) {
            return Collections.emptyList();
        }
        List<Integer> companyIds = courierCompanies.stream()
                .map(CourierCompany::getCompanyId)
                .collect(Collectors.toList());

        LambdaQueryWrapper<Courier> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Courier::getCompanyId, companyIds);
        List<Courier> allCouriers = courierMapper.selectList(queryWrapper);

        Map<Integer, Long> companyIdToCountMap = allCouriers.stream()
                .collect(Collectors.groupingBy(Courier::getCompanyId, Collectors.counting()));

        for (CourierCompany company : courierCompanies) {
            company.setCourierNumber(companyIdToCountMap.getOrDefault(company.getCompanyId(), 0L));
        }
        return courierCompanies;
    }

    /**
     * 添加/修改
     *
     * @param controller
     * @return
     */
    @Override
    @Transactional
    public Integer addOrUp(CourierCompany controller) {
        boolean saved = saveOrUpdate(controller);
        if (!saved) {
            throw new ServiceException("添加/修改失败", 201);
        }
        return controller.getCompanyId();
    }

    /**
     * 根据公司id获取到这个公司的员工数量
     *
     * @param companyId
     * @return
     */
    @Override
    public Map<String, Integer> courierNumber(Integer companyId) {
        List<Courier> couriers = courierMapper.selectList(
                new LambdaQueryWrapper<Courier>().eq(Courier::getCompanyId, companyId)
        );

        int deliveryNumber = couriers.stream()
                .mapToInt(Courier::getDeliveryQuantity)
                .sum();


        List<CourierCompanyMessage> courierCompanyMessages = courierCompanyMessageMapper.selectList(
                new LambdaQueryWrapper<CourierCompanyMessage>().eq(CourierCompanyMessage::getCompanyId, companyId)
        );

        Map<String, Integer> resultMap = new HashMap<>();
        resultMap.put("courierNumber", couriers.size());
        resultMap.put("deliveryNumber", deliveryNumber);
        resultMap.put("messageNumber", courierCompanyMessages.size());

        return resultMap;
    }


}
